package college;

import clientserver.Client;
import clientserver.Packet;
import clientserver.QueryResult;




/**
 * Request class   uses the MysqL requests table:
 * <p> Used for reading and writing a request from the table located in the database.
 * @param id - Current request ID.
 * @param isystem - Information system that the change should apply to.
 * @param changedesc - Request's change description given by client.
 * @param currsituation - Current information system status.
 * @param  explanation - Reasoning behind current request.
 * @param supervisor - selected supervisor for current request.
 * @param initiator - Request initiator.
 * @author Maxim Rahman
 * @see collegeObject
 *
 */

public class Request extends collegeObject {



	/*Class variables  */
	private int id;
	private String isystem; // information system choosed for request 
	private String  changedesc;  // description of change request
	private String   currsituation;    // Description of current situation in information system
	private String   explanation;  // explanation - why we need to change current situation
	private String  supervisor;  // request supervisor
	

	 private String  initiator; //  Request Initialor login
	 
	 /**
	  * Reuqest constructor - builds a request class type instance.
	  * @param sys - The information system selected in the request.
	  * @param change - The requested change that is implied.
	  * @param situation - Current status of the selected information system.
	  * @param expl - Client's explanation (reason) for current request.
	  */

	 public   Request( String sys, String change, String situation, String expl ) 
	 {
		 isystem=sys;
		 changedesc=change;
		 currsituation=situation;
		 explanation=expl;
		 
	 }
	 /**
	  * Create a blank request for user C.
	  * @param c
	  */
	 public Request(Client c)
	 {
		 
		 client=c;
		 resetFields();
	 }
	 
	
	 private void resetFields()
	 {
		 isystem=changedesc=currsituation="";
		 explanation=supervisor=initiator="";
	 }
	 
	 
	 
	 
	 /////Getters and Setters////////////////////////////////////////////////////
	public String getIsystem() {
		return isystem;
	}

	public void setIsystem(String isystem) {
		this.isystem = isystem;
	}




	public String getChangedesc() {
		return changedesc;
	}

	public void setChangedesc(String changedesc) {
		this.changedesc = changedesc;
	}

	public String getCurrsituation() {
		return currsituation;
	}

	
 public void setId( int num)
 {
	 id=num;
 }
	public int getId() {
		return id;
	}

	public String getExplanation() {
		return explanation;
	}

	public void setExplanation(String explanation) {
		this.explanation = explanation;
	}
	
	public String getInitiator() {
		return initiator;
	}

	public void setInitiator(String initiator) {
		this.initiator = initiator;
	}
	
	
	public String getSupervisor() {
		if (supervisor==null) return "None";
		return supervisor;
	}



	public void setSupervisor(String supervisor) {
		this.supervisor = supervisor;
	}

	
	/**
	 * Converts Requests fields  in String report about request
	 * also with Request progress details from request_details table
	 */
	public String toString()
	{
		User u= new User();
		u.setClient(client);
		u.setLogin(initiator);
		u.loadfromdb(); // load initiator full name from Data Base
		RequestTreatment rt =new RequestTreatment(client, id);
		rt.loadfromdb();// load request details from DB
		String rep="Request ID N"+id+
				"\nChange Request in Inormation System: "+isystem+
				"\nChange Request Initiator: "+u.getFullname()+
				"\nRequest  Change Description: "+changedesc+
				"\n Current Situation in the System: "+currsituation+
				"\nExplanation for Change Request: "+explanation+
				"\nCurrent Supervisor for Request: "+getSupervisor()+
				"\n--------------------------------------------------\n"+rt.toString();
		
		
		return rep;
	}
//////////////////////////////////////////////////////////////////////////////////////Change Request functions


	
	/**Add request to table requests
	 * 
	 * @return request id assigned automatically by mySql
	 */
	public void addToDb()
	{
		
		String q="INSERT INTO requests (id,infosys,change_desc,curr_sit_desc,explanation,initiator,supervisor) VALUES("+general.addapostrophs(String.valueOf(id))+","+general.addapostrophs(isystem)+","+general.addapostrophs(changedesc)+","+general.addapostrophs(currsituation)+","+general.addapostrophs(explanation)+","+general.addapostrophs(initiator)+",null)";
			Packet  p;
		p=new Packet(q);
		p.setUpdatePacket();
	
		client.SendPacketNowait(p);
		
			
	}
	
	/**
	 * Loads a request tuple from the database, identified by its ID.
	 * @param rid - Requests ID.
	 */
	public void loadFromDb( int rid)
	{
	id=rid;
	String sid=Integer.toString(rid);
	String q=" SELECT * FROM requests WHERE id="+general.addapostrophs(sid);
	Packet p=new Packet(q);
	QueryResult qr;
	qr=(QueryResult) client.SendPacketAndWait(p);
	
	isystem=qr.getString("infosys");
	 changedesc=qr.getString("change_desc");
  currsituation=qr.getString("curr_sit_desc");
		explanation=qr.getString("explanation");
	 setSupervisor(qr.getString("supervisor"));
	 initiator=qr.getString("initiator");
	}
	
	/**
	 * Update the supervisor of selected (by request ID) request.
	 */
 public void  updateSupervisor()
 {
	  String rid=""+id;
		String q="UPDATE requests SET supervisor="+general.addapostrophs(supervisor)+" WHERE id="+general.addapostrophs(rid);
		System.out.println(q);
		Packet  p;
		p=new Packet(q);
		p.setUpdatePacket(); 
		client.SendPacketNowait(p);
	 
 }

	
}
